﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DONGACS.Common;
using QLPKWeb.Models.FormBean.NhanVienModel;
using QLPKWeb.Models.LinQ.NhanVien;
using Webdiyer.WebControls.Mvc;

namespace QLPKWeb.BussinessDelegate.NhanVienDelegate
{
    public class ThongTinLienQuanDelegate
    {
        private NhanVienDataContext db = new NhanVienDataContext();
        int _UserId = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
        int _DeparmentId = Convert.ToInt32(HttpContext.Current.Session["DEPARTMENT"]);
        DateTime _DateNow = DateTime.Now;

        public hs_ThongTinLienQuan Insert(hs_ThongTinLienQuan obj)
        {
            try
            {
                obj.CreateBy = _UserId;
                obj.CreateDate = _DateNow;
                db.hs_ThongTinLienQuans.InsertOnSubmit(obj);
                db.SubmitChanges();
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public bool UpdatethongTinLienQuan(hs_ThongTinLienQuan IdThongTinLienQuan)
        {
            try
            {
                hs_ThongTinLienQuan LuongCoBanUpdate =
                    db.hs_ThongTinLienQuans.Single(p => p.IdNhanVien == IdThongTinLienQuan.IdNhanVien);
                if (LuongCoBanUpdate != null && LuongCoBanUpdate.IsDelete == false)
                {
                    LuongCoBanUpdate.Id = IdThongTinLienQuan.Id;
                    LuongCoBanUpdate.NgayNhanViecTheoHopDong = IdThongTinLienQuan.NgayNhanViecTheoHopDong;
                    LuongCoBanUpdate.NgayThuViec = IdThongTinLienQuan.NgayThuViec;
                    LuongCoBanUpdate.HopDong6Thang = IdThongTinLienQuan.HopDong6Thang;
                    LuongCoBanUpdate.HopDong12Thang = IdThongTinLienQuan.HopDong12Thang;
                    LuongCoBanUpdate.UpdateDate = IdThongTinLienQuan.UpdateDate;
                    LuongCoBanUpdate.UpdateBy = IdThongTinLienQuan.UpdateBy;
                    db.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception)
            {
                return false;
            }
        }
        public hs_ThongTinLienQuan GetThogntinLienQuanById(string id)
        {
            try
            {
                hs_ThongTinLienQuan item = this.db.hs_ThongTinLienQuans.Single(p => p.IdNhanVien == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public hs_ThongTinLienQuan BuildnewLuongCoBan(ThongTinThemHSNhanVienForm form)
        {
            hs_ThongTinLienQuan newThongTinLienQuan = new hs_ThongTinLienQuan();
            newThongTinLienQuan.IdNhanVien = form.IdNhanVien;
            newThongTinLienQuan.NgayNhanViecTheoHopDong = form.NgayNhanViecTheoHopDong;
            newThongTinLienQuan.HopDong6Thang = form.HopDong6Thang;
            newThongTinLienQuan.NgayThuViec = form.NgayThuViec;
            newThongTinLienQuan.HopDong12Thang = form.HopDong12Thang;

            newThongTinLienQuan.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newThongTinLienQuan.CreateDate = DateTime.Now;
            newThongTinLienQuan.IsDelete = false;
            return newThongTinLienQuan;
        }


        public hs_ThongTinLienQuan BuildEditNhanVien(ThongTinThemHSNhanVienForm form)
        {
            hs_ThongTinLienQuan newThongTinLienQuan = new hs_ThongTinLienQuan();
            newThongTinLienQuan.IdNhanVien = form.IdNhanVien;
            newThongTinLienQuan.NgayNhanViecTheoHopDong = form.NgayNhanViecTheoHopDong;
            newThongTinLienQuan.HopDong6Thang = form.HopDong6Thang;
            newThongTinLienQuan.NgayThuViec = form.NgayThuViec;
            newThongTinLienQuan.HopDong12Thang = form.HopDong12Thang;

            newThongTinLienQuan.UpdateDate = DateTime.Now;
            newThongTinLienQuan.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newThongTinLienQuan;
        }


        public ThongTinThemHSNhanVienForm ConvertToForm(hs_ThongTinLienQuan obj)
        {
            ThongTinThemHSNhanVienForm form = new ThongTinThemHSNhanVienForm();
            form.Id = obj.Id;
            form.IdNhanVien = obj.IdNhanVien;
            form.NgayNhanViecTheoHopDong = obj.NgayNhanViecTheoHopDong;
            form.NgayThuViec = obj.NgayThuViec;
            form.HopDong6Thang = obj.HopDong6Thang;
            form.HopDong12Thang = obj.HopDong12Thang;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
        public hs_ThongTinLienQuan ConvertToLinq(ThongTinThemHSNhanVienForm form)
        {
            hs_ThongTinLienQuan obj = new hs_ThongTinLienQuan();
            obj.Id = form.Id;
            obj.IdNhanVien = form.IdNhanVien;
            obj.NgayNhanViecTheoHopDong = form.NgayNhanViecTheoHopDong;
            obj.NgayThuViec = form.NgayThuViec;
            obj.HopDong6Thang = form.HopDong6Thang;
            obj.HopDong12Thang = form.HopDong12Thang;
            return obj;
        }
    }
}